<template>
  <div>
    <el-dialog
      v-model="echartsDrawerShow"
      v-if="echartsDrawerShow"
      top="3%"
      width="800px"
      :title="drawerText"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <el-tabs v-model="elmenuActive" class="demo-tabs" v-if="!isProduct">
        <el-tab-pane label="用电分析" name="1"> </el-tab-pane>
        <el-tab-pane label="所有设备" name="2"
          ><all-Equipment></all-Equipment
        ></el-tab-pane>
        <el-tab-pane label="所有警告" name="3">
          <all-Alarm
            :activeName="'first'"
            :isSearch="1"
            :ismodule="1"
          ></all-Alarm
        ></el-tab-pane>
      </el-tabs>
      <div style="width: 100%" v-if="elmenuActive == 1">
        <echarts-Drawer v-if="electroShow"></echarts-Drawer>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
import { ref, reactive, onMounted } from "vue";
import parsing from "../../assets/js/parsing";
import echartsDrawer from "./echarts_drawer.vue";
import allEquipment from "./all_equipment.vue";
import allAlarm from "./all_Alarm.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import * as echarts from "echarts";
import api from "../../api/api";
export default {
  components: { echartsDrawer, allAlarm, allEquipment },
  setup(props, conts) {
    const drawerText = conts.attrs.drawerText;
    const echartsDrawerShow = conts.attrs.echartsDrawerShow;
    const isProduct = ref(conts.attrs.isProduct);
    const registrationsUser = ref(null);
    const elmenuActive = ref("1");
    onMounted(async () => {});
    const handleClose = () => {
      conts.emit("handleClose", false);
    };
    const electro_From = reactive({
      queryStartDate: parsing.getday2(), //2021-10-20
      queryEndDate: parsing.getcurrentdate(),
      queryAreaCode: "3502010701",
      queryDate: parsing.getcurrentdate(),
      queryRegionId: "100008064",
    });
    //用电分析 按日
    const electroList = ref([]);
    const electroShow = ref(true);
    const getelectro = () => {
      electroList.value = [];
      electroShow.value = false;
      api
        .postFormAPI("data/regionElectricity/sumDaily", electro_From)
        .then((res) => {
          if (res.data.code == 1) {
            for (var i = 0; i < res.data.data.length; i++) {
              res.data.data[i].value = res.data.data[i].alarmNum;
            }
            electroList.value = res.data.data;
            electroShow.value = true;
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    };
    // 按月
    const getsumMonthly = () => {
      electroList.value = [];
      electroShow.value = false;
      api
        .postFormAPI("data/regionElectricity/sumMonthly", electro_From)
        .then((res) => {
          if (res.data.code == 1) {
            for (var i = 0; i < res.data.data.length; i++) {
              res.data.data[i].value = res.data.data[i].alarmNum;
            }
            electroList.value = res.data.data;
            electroShow.value = true;
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    };
    const opens = () => {
      getelectro();
    };

    const active_data = ref(2);
    const handleSearch = () => {
      if (active_data.value == 2) {
        getelectro();
      } else if (active_data.value == 3) {
        getsumMonthly();
      } else if (active_data.value == 4) {
        getsumMonthly();
      } else {
        getelectro();
      }
    };
    const datavalue = ref([parsing.getcurrentdate(), parsing.getcurrentdate()]);
    const is_week_disabled = ref(true);

    const is_data_disabled = ref(false);
    const changedata = (res) => {
      // var numberdays = parsing.DateDifference(res[1], res[0]);
      setdisabled(res[1], res[0]);
      electro_From.queryStartDate = res[0];
      electro_From.queryEndDate = res[1];
      electro_From.queryDate = res[1];
    };
    const setdisabled = (data1, data2) => {
      var numberdays = parsing.DateDifference(data1, data2);
      if (numberdays >= 1 && numberdays == 1) {
        is_data_disabled.value = false;
        is_week_disabled.value = true;
      } else if (numberdays >= 2 && numberdays <= 7) {
        is_week_disabled.value = true;
        is_data_disabled.value = false;
      } else if (numberdays >= 8 && numberdays <= 30) {
        is_week_disabled.value = false;
        is_data_disabled.value = false;
      } else if (numberdays >= 31 && numberdays <= 180) {
        is_week_disabled.value = false;

        is_data_disabled.value = true;
      } else if (numberdays >= 181 && numberdays <= 366) {
        is_week_disabled.value = true;
        is_data_disabled.value = true;
      } else {
        is_week_disabled.value = true;
        is_data_disabled.value = true;
      }
    };
    const handledata = (index) => {
      active_data.value = index;

      if (index == 2) {
        electro_From.queryStartDate = parsing.getday2();
        datavalue.value = [parsing.getday2(), parsing.getcurrentdate()];
        getelectro();
      } else if (index == 3) {
        console.log(parsing.getLastSixMon());
        electro_From.queryStartDate = parsing.getLastSixMon();
        datavalue.value = [parsing.getLastSixMon(), parsing.getcurrentdate()];
        // getsumWeekly();
        getsumMonthly();
      } else if (index == 4) {
        electro_From.queryStartDate = parsing.getLastSixMon();
        datavalue.value = [parsing.getLastSixMon(), parsing.getcurrentdate()];
        getsumMonthly();
      }
    };
    const handleExport = () => {
      getdownload();
    };
    const getdownload = (code) => {
      axios({
        method: "GET",
        url: api.getUrl() + "backup/excel/download?code=" + code,
        responseType: "blob",
      })
        .then((resdata) => {
          download(resdata.data, "用电分析统计.xlsx");
        })
        .catch((err) => {
          if (!handleError(err.response)) {
            error(err);
          }
        });
    };
    return {
      isProduct,
      elmenuActive,
      active_data,
      is_data_disabled,
      is_week_disabled,
      registrationsUser,
      drawerText,
      electroList,
      datavalue,
      electroShow,
      echartsDrawerShow,
      handleClose,
      opens,
      handleSearch,
      changedata,
      handledata,
      handleExport,
    };
  },
};
</script>
<style lang="less" scoped>
@import "../../assets/css/common.css";
&:deep(.el-dialog__body) {
  padding: 15px 20px;
  overflow: auto;
}
.el-drawer-content {
  padding-bottom: 30px;
}
&:deep(.el-container) {
  width: 1300px;
}
&:deep(.el-drawer__header) {
  color: #fff;
  border-bottom: 1px solid rgb(42, 99, 206);
  padding-bottom: 20px;
  font-size: 32px;
}
.cen-header {
  background-color: rgba(30, 81, 181, 1);
  width: 100%;
  margin-left: 20px;
  height: 100px;
}
.cen-header1 {
  background-color: rgb(3, 64, 185);
  height: 30px;
  color: #fff;
  font-size: 14px;
  line-height: 30px;
  margin-left: 20px;
  width: 100%;
}
.cen-header1 .title {
  padding-left: 20px;
}

.icon_img {
  width: 20px;
}
.source_head_icon {
  min-height: 50px;
  max-height: 60px;
  min-width: 50px;
  max-width: 60px;
}
.model_head {
  color: #fff;
  font-size: 14px;
  font-family: Source Han Sans CN;
  font-weight: 400;
  color: #ffffff;
  line-height: 25px;
  margin-left: 30px;
}
.model_head > div {
  margin-left: 10px;
}
.model_head .model_value {
  font-size: 14px;
  font-family: Source Han Sans CN;
  font-weight: bold;
}
.curve_echarts {
  background-color: #fff;
  margin-top: 10px;
}
&:deep(.el-menu) {
  background-color: rgba(30, 81, 181, 1);
}
&:deep(.el-menu-item) {
  color: #fff;
}
&:deep(.el-menu-item.is-active) {
  background-color: rgb(41 114 255);
  color: #fff;
}
&:deep(.el-menu-item:hover) {
  background-color: rgba(214, 214, 214, 0.342);
  color: #fff;
}
.daterange {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  margin: 15px;
}
.daterange > div:nth-child(1) {
  margin-right: 10px;
}

.disabled_active {
  background: rgba(128, 128, 128, 0.774);
  border-color: rgba(128, 128, 128, 0.774);
}
.cen-header2 {
  background-color: #fff;
  margin-top: 20px;
  margin-left: 20px;
  width: 100%;
  height: 180px;
  div {
    color: #333;
  }
}
.source_head_icon2 {
  width: 150px;
  height: 150px;
}
.el-menu-vertical {
  min-height: 500px;
}
</style>
